package org.example.zhihuan.control;

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.example.zhihuan.services.User;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static org.example.zhihuan.control.Result.PARAMETER_WRONG;

@WebServlet(name = "UserServlet",urlPatterns = "/user")
public class UserServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setHeader("Content-Type", "application/json;charset=UTF-8");
        ObjectMapper mapper = new ObjectMapper();
        mapper.disable(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT);
        Map<String, Object> map = mapper.readValue(req.getInputStream(), HashMap.class);
        try {
            switch (map.get("_type").toString()) {
                case "register":
                    register(map, resp);
                    break;
                case "creatGoods":
                    createGoods(map, resp);
                    break;
                case "login":
                    login(map, resp);
                    break;
                case "goodsUp":
                    userGoodsUp(map, resp);
                    break;
                case "goodsDown":
                    userGoodsDown(map, resp);
                    break;
                case "createOrder":
                    userCreateOrder(map, resp);
                    break;
                case "modifyGoods":
                    userModifyGoods(map, resp);
                    break;
                case "cancel":          //用户注销
                    userCancel(map, resp);
                    break;
                case "assessmentDelete":
                    userAssDelete(map, resp);
                    break;
                default:
                    resp.getWriter().print(new Result(999, "未知", null));
            }
        } catch (Exception e) {
            resp.getWriter().print(new Result(432, e.getMessage(), null));
        }
    }

    /**
     * 注册
     * @param map
     * @param resp
     * @throws IOException
     */
    private void register(Map<String, Object> map, HttpServletResponse resp) throws IOException {
        if (!(map.containsKey("name") && map.containsKey("passwd"))) {
            resp.getWriter().print(new Result(PARAMETER_WRONG, "参数不正确", null));
        }
        User user = User.register(map.get("name").toString(), map.get("passwd").toString());
        Result result;
        if (user == null) {
            result = Result.fail(Result.NOT_ALLOW, "未能创建用户对象", null);
        } else {
            result = Result.ok(user.getUserId());
        }
        resp.getWriter().print(result);
    }

    /**
     * 创建商品
     * @param map
     * @param resp
     * @throws IOException
     */
    private void createGoods(Map<String, Object> map, HttpServletResponse resp) throws IOException {
        if (!(map.containsKey("userId") && (map.containsKey("goodsName") && map.containsKey("goodsType")) && map.containsKey("price") && map.containsKey("goodsContent"))) {
            resp.getWriter().print(new Result(PARAMETER_WRONG, "参数不正确", null));
        }
        User user = User.getUser((Integer) map.get("userId"));
        Result result;
        if (user == null) {
            result = Result.fail(Result.NOT_ALLOW, "用户未找到", null);
        } else {
            result = Result.ok(user.getUserId());
        }
        user.createGoods(map.get("goodsName").toString(), (Integer) map.get("goodsType"),
                (double) map.get("price"), map.get("goodsContent").toString());
        resp.getWriter().print(result);
    }

    /**
     * 登录
     * @param map
     * @param resp
     * @throws IOException
     */
    public void login(Map<String, Object> map, HttpServletResponse resp) throws IOException {
        if (!(map.containsKey("name") && map.containsKey("passwd"))) {
            resp.getWriter().print(new Result(PARAMETER_WRONG, "参数不正确", null));
        }
        Result result;
        if (User.login(map.get("name").toString(), map.get("passwd").toString())) {
            result = Result.ok(true);
        } else {
            result = Result.fail(Result.PASS_WRONG, "密码不正确", null);
        }
        resp.getWriter().print(result);
    }

    /**
     * 商品上架
     * @param map
     * @param resp
     * @throws IOException
     */
    public void userGoodsUp(Map<String, Object> map, HttpServletResponse resp) throws IOException {
        if (!(map.containsKey("userId") && map.containsKey("goodsId"))) {
            resp.getWriter().print(new Result(PARAMETER_WRONG, "参数不正确", null));
        }
        Result result;
        User user = User.getUser((Integer) map.get("userId"));
        user.userGoodsUp((Integer) map.get("goodsId"));
        result = Result.ok(user.getUserId());
        resp.getWriter().print(result);
    }

    /**
     * 商品下架
     * @param map
     * @param resp
     * @throws IOException
     */
    public void userGoodsDown(Map<String, Object> map, HttpServletResponse resp) throws IOException {
        if (!(map.containsKey("userId") && map.containsKey("goodsId"))) {
            resp.getWriter().print(new Result(PARAMETER_WRONG, "参数不正确", null));
        }
        Result result;
        User user = User.getUser((Integer) map.get("userId"));
        user.userGoodsDown((Integer) map.get("goodsId"));
        result = Result.ok(user.getUserId());
        resp.getWriter().print(result);
    }

    /**
     * 创建订单
     * @param map
     * @param resp
     * @throws IOException
     */
    private void userCreateOrder(Map<String, Object> map, HttpServletResponse resp) throws IOException {
        if (!(map.containsKey("userId") &&  map.containsKey("sellerId"))
                && map.containsKey("sellGoods") && map.containsKey("buyGoods")) {
            resp.getWriter().print(new Result(PARAMETER_WRONG, "参数不正确", null));
        }
        Result result;
        List<Integer> sellGoods = (List<Integer>) map.get("sellGoods");
        List<Integer> buyGoods = (List<Integer>) map.get("buyGoods");
        User user = User.getUser((Integer) map.get("userId"));
        user.userCreateOrder((Integer) map.get("sellerId"),sellGoods,buyGoods);
        result = Result.ok(user.getUserId());
        resp.getWriter().print(result);
    }

    /**
     * 修改商品
     * @param map
     * @param resp
     * @throws IOException
     */
    public void userModifyGoods(Map<String, Object> map, HttpServletResponse resp) throws IOException {
        if (!(map.containsKey("userId") &&  map.containsKey("goodsId"))
                && map.containsKey("goodsName") && map.containsKey("goodsType")&& map.containsKey("price")
                &&map.containsKey("goodsContent")) {
            resp.getWriter().print(new Result(PARAMETER_WRONG, "参数不正确", null));
        }
        Result result;
        User user = User.getUser((Integer) map.get("userId"));
        user.userModifyGoods((Integer) map.get("goodsId"),map.get("goodsName").toString(),(Integer)map.get("goodsType")
        ,(double) map.get("price"),map.get("goodsContent").toString());
        result = Result.ok(user.getUserId());
        resp.getWriter().print(result);
    }

    /**
     * 注销
     * @param map
     * @param resp
     * @throws IOException
     */
    public void userCancel(Map<String, Object> map, HttpServletResponse resp) throws IOException {
        if (!(map.containsKey("userId") && map.containsKey("ban"))) {
            resp.getWriter().print(new Result(PARAMETER_WRONG, "参数不正确", null));
        }
        User user = User.getUser((Integer) map.get("userId"));
        user.userCancel((boolean) map.get("ban"));
        Result result;
        result = Result.ok(user.getUserId());
        resp.getWriter().print(result);
    }

    /**
     * 删评论
     * @param map
     * @param resp
     * @throws IOException
     */
    public void userAssDelete(Map<String, Object> map, HttpServletResponse resp) throws IOException {
        if (!(map.containsKey("userId") && map.containsKey("assId"))) {
            resp.getWriter().print(new Result(PARAMETER_WRONG, "参数不正确", null));
        }
        User user = User.getUser((Integer) map.get("userId"));
        user.userAssDelete((Integer)map.get("assId"));
        Result result;
        result = Result.ok(user.getUserId());
        resp.getWriter().print(result);




    }


}